package natappvip.liujinliang.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import natappvip.liujinliang.model.FileEle;

/**
 * @ClassName:  DataManipulationLang   
 * @Despt: DML语言类 
 * @Auth: Liujinliang 
 * @Date: 2018年7月29日 下午9:39:11
 */
public class DataManipulationLang {
	
	private static Logger logger = LoggerFactory.getLogger(DataManipulationLang.class);

	public static ResultSet query(Connection conn, String sql, Object[] obj) {
		PreparedStatement pStatement = null;
		ResultSet resultSet = null;
		if (sql != null) {
			if (conn != null) {
				try {
					pStatement = conn.prepareStatement(sql);
					if (obj != null) {
						for (int i = 0; i < obj.length; i++) {
							pStatement.setObject(i + 1, obj[i]);
						}
					}
					resultSet = pStatement.executeQuery(); 
				} catch (SQLException e) {
					logger.info("数据库查询语句执行错误，错误信息{}", e.getMessage());
				} 
			}
		} 
		return resultSet;
	}
	
	public static int update(Connection conn, String sql, Object[] obj) {
		int flag = -1;
		PreparedStatement pStatement = null;
		if (sql != null && obj != null) {
			if (conn != null) {
				try {
					pStatement = conn.prepareStatement(sql);
					for (int i = 0; i < obj.length; i++) {
						pStatement.setObject(i + 1, obj[i]);
					}
					flag = pStatement.executeUpdate();
				} catch (SQLException e) {
					logger.info("数据库更新语句执行错误，错误信息{}", e.getMessage());
				} 
			} 
		}
		return flag;
	}
	
	public static ResultSet query(Connection conn, String sql, String str) {
		Object[] obj = {str};
		return query(conn, sql, obj);
	}

	public static int update(Connection conn, String sql, FileEle ele) {
		Object[] obj = {ele.getUuid(), ele.getSize(), ele.getCreateTime(), ele.getExisted()};
		return update(conn, sql, obj);
	}
	
	public static int update(Connection conn, String sql, String str) {
		Object[] obj = {str};
		return update(conn, sql, obj);
	}
}
